package com.example.jn_dxs.view;

import android.content.Context;
import android.util.Log;
import android.widget.TextView;

import com.example.jn_dxs.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;

import java.util.ArrayList;

public class MarkerViews extends MarkerView {

    private TextView tvContent;
    LineChart lineChart;//图表控件
    ArrayList<String> xvalue;
    private String Type;
    /**
     * Constructor. Sets up the MarkerView with a custom layout resource.
     *
     * @param context
     * @param layoutResource the layout resource to use for the MarkerView
     */
    public MarkerViews(Context context, int layoutResource, LineChart lineChart, ArrayList<String> xvalue,String Type) {
        super(context, R.layout.maekertextview);
        tvContent = (TextView) findViewById(R.id.tvContent);
        this.lineChart=lineChart;
        this.xvalue=xvalue;
        this.Type=Type;
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight) {

        LineData lineData=lineChart.getLineData();

        LineDataSet set=(LineDataSet)lineData.getDataSetByIndex(0);

        int DataSetIndex=highlight.getDataSetIndex();
        int index = 0;
        if (DataSetIndex==0){
            index= set.getEntryIndex(e);//根据点击的该条折线的点，获取当前Y轴数据对应的index值，
        }
        //根据index值，分别获取当前X轴上对应的两条折线的Y轴的值
        Entry entry=set.getEntryForIndex(index);//
        Log.i("x,y轴","/"+index+"/"+DataSetIndex);

        tvContent.setText("时间："+xvalue.get(index)+"\n"+Type+entry.getY()); // set the entry-value as the display text


        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());

    }
}

